package com.neuedu.dao;

import com.neuedu.common.DBUtils;
import com.neuedu.entity.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
管理员数据访问
 */
public class AdminDao {

     /*
    插入
     */
     public boolean insert(Admin admin){
         String sql="insert into admin (adminName,password)VALUES(?,?)";

         try {
             Connection connection=DBUtils.getConnection();
             PreparedStatement ps=connection.prepareStatement(sql);
             ps.setString(1, admin.getAdminName());
             ps.setString(2,admin.getPassword());

             if(ps.executeUpdate()>0)
             {
                 DBUtils.close(connection,ps,null);
                 return true;
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
         return false;
     }
     /*
     查询所有记录
      */
     public List<Admin> findByAll(){
             String sql="select *from admin";
         List<Admin> list=new ArrayList<>();
         Connection connection= null;
         try {
             connection = DBUtils.getConnection();
             PreparedStatement ps=connection.prepareStatement(sql);
             //查询数据库得到结果集
             ResultSet rs=ps.executeQuery();
             while(rs.next()){
                 /*System.out.print(rs.getInt(1));
                 System.out.println(rs.getString(2));*/
                 Admin admin=new Admin();
                 admin.setId(rs.getInt(1));
                 admin.setAdminName(rs.getString(2));
                 admin.setPassword(rs.getString(3));
                 admin.setCreatetime(rs.getTimestamp(4));
                 list.add(admin);
             }
             DBUtils.close(connection,ps,rs);
         } catch (SQLException e) {
             e.printStackTrace();
         }
           return list;
     }
     /*
     修改，先查后改
      */
     public boolean update(Admin admin){
         Admin admin1=this.getById(admin.getId());
         if(null!=admin1)
         {
             try {
                 String sql="update admin set adminName=?,password=?," +
                         "updatetime=?,lastlogintime=?where id=?";
                 Connection connection = DBUtils.getConnection();
                 PreparedStatement ps = connection.prepareStatement(sql);
                 ps.setString(1,admin.getAdminName());
                 ps.setString(2,admin.getPassword());
                 ps.setDate(3,new java.sql.Date(admin.getCreatetime().getTime()));
                 ps.setDate(4,new java.sql.Date(admin.getLastlogintime().getTime()));
                 ps.setInt(5,admin.getId());
                 if(ps.executeUpdate()>0)
                 {
                     DBUtils.close(connection,ps,null);
                     return true;
                 }
             }catch (Exception e)
             {
              e.getStackTrace();
             }
         }
      return false;
     }

     public Admin getById(Integer id){
         String sql="select id,adminName,password,createtime,updatetime,lastlogintime\n" +
                 "from admin where id=?";
         try {
             Connection connection = DBUtils.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql);
             ps.setInt(1,id);
             ResultSet rs=ps.executeQuery();
             Admin admin=new Admin();
             if(rs.next()){
                 admin.setId(rs.getInt(1));
                 admin.setAdminName(rs.getString(2));
                 admin.setPassword(rs.getString(3));
                 admin.setCreatetime(rs.getTimestamp(4));
                 admin.setUpdatetime(rs.getTimestamp(5));
                 admin.setLastlogintime(rs.getTimestamp(6));
                 DBUtils.close(connection,ps,rs);
                 return admin;
             }
             else {
                 return null;
             }
         }catch (Exception e)
         {
             e.getStackTrace();
             return null;
         }
     }

/*
批量删除
 */
        public boolean delete(Integer[] ids){
         String sql="delete from admin where id in(";
         try {
            for (Integer id:ids){
                sql+="?,";
          }
        sql=sql.substring(0,sql.length()-1)+")";
        System.out.print(sql);
        Connection connection = DBUtils.getConnection();
        PreparedStatement ps = connection.prepareStatement(sql);
        int count=ids.length;
        for (int i=0;i<count;i++){
            ps.setInt(i+1,ids[i]);
        }
        if(ps.executeUpdate()>0){
            return  true;
        }
       // ps.setInt(int);
         }catch (Exception e){
             e.getStackTrace();
         }

         return false;
        }
/*
条件查询
 */
        public List<Admin> findByWhere(String where)
        {

            String sql="select id,adminName,password,createtime,updatetime,lastlogintime\n" +
                    "from admin where 1=1"+where;
            List<Admin> list=new ArrayList<>();
            try {
                Connection connection = DBUtils.getConnection();
                PreparedStatement ps = connection.prepareStatement(sql);
                ResultSet rs=ps.executeQuery();
                while(rs.next()){
                    Admin admin=new Admin();
                    admin.setId(rs.getInt(1));
                    admin.setAdminName(rs.getString(2));
                    admin.setPassword(rs.getString(3));
                    admin.setCreatetime(rs.getTimestamp(4));
                    admin.setUpdatetime(rs.getTimestamp(5));
                    admin.setLastlogintime(rs.getTimestamp(6));
                    list.add(admin);
                }
                DBUtils.close(connection,ps,rs);

            }catch (Exception e){
                e.getStackTrace();
            }
            return  list;

        }
/*
主函数
 */
     public static  void main(String[] args){
         AdminDao adminDao=new AdminDao();
         /*Admin admin=new Admin();
         admin.setAdminName("zhangsan");
         admin.setPassword("000000");
         admin.setCreatetime(new Date());
         adminDao.insert(admin);
         List<Admin> admins=adminDao.findByAll();
         for(Admin admin:admins){
             System.out.println(admin.getId()+"->"+admin.getAdminName());
         }
         Admin admin=adminDao.getById(1);
         if (null!=admin)
         {
             System.out.print(admin.getAdminName());
         }
         Admin admin=new Admin();
         admin.setId(1);
         admin.setAdminName("admin");
         admin.setPassword("000000");
         admin.setCreatetime(new Date());
         admin.setLastlogintime(new Date());
         adminDao.update(admin);
         Integer[] ids={7,8,9};
         adminDao.delete(ids);*/
         Admin admin=new Admin();
         admin.setAdminName("admin");
         admin.setPassword("000000");
         String where=" and adminName='"+ admin.getAdminName()+"' AND password='"+admin.getPassword()+"'";
         List<Admin> list=adminDao.findByWhere(where);
         // List<Admin> list=adminDao.findByWhere(" and adminName='"+ admin.getAdminName()+"'");
         if(list.size()>0){

             //System.out.print("该用户已存在");
             System.out.print("登陆成功");
         }

     }
}
